package yl.hs.bmipfull.service.laboratorydata.imple;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import yl.hs.bmipfull.daos.laboratorydata.ILabConfigSpecimenDao;
import yl.hs.bmipfull.pojo.laboratorydata.LabConfigSpecimenEntity;
import yl.hs.bmipfull.pojo.laboratorydata.LabExperimentDefinitionlibsEntity;
import yl.hs.bmipfull.service.laboratorydata.ILabConfigSpecimenService;
import yl.hs.bmipfull.utils.DataState;
import yl.hs.bmipfull.utils.SearchFilterEngine;
import yl.hs.bmipfull.utils.Tl;
import yl.hs.bmipfull.utils.datastate.ActionType;
import yl.hs.bmipfull.viewmodels.PageQuery;
import yl.hs.bmipfull.viewmodels.PageResult;

import java.util.List;
import java.util.Map;

/**
 * @author 吴高丰
 * @apiNote 试配试件信息服务层接口实现
 * @since 2022年07月20日 09时17分44秒 <br/>
 */
@Service
public class LabConfigSpecimenServiceImpl extends LaboratorydataBaseService implements ILabConfigSpecimenService {

    @Autowired
    ILabConfigSpecimenDao dao;

    @Override
    public List<LabConfigSpecimenEntity> queryList(PageQuery pageQuery) throws Exception {
        pageQuery.checkSortSides(LabConfigSpecimenEntity.class);
        pageQuery.getQuery().put("sortKey", String.join(",", pageQuery.getSortSides()));
        pageQuery.getQuery().put("unitSNArray", getCompanySNList());
        var filters = SearchFilterEngine.generateQueryList(pageQuery.getFilters(), LabConfigSpecimenEntity.class);
        return dao.queryList(pageQuery.getQuery(), filters);
    }

    @Override
    public PageResult<LabConfigSpecimenEntity> queryPage(PageQuery pageQuery, int rowNum, int page) throws Exception {
        pageQuery.checkSortSides(LabConfigSpecimenEntity.class);
        pageQuery.getQuery().put("querySkip", (page - 1) * rowNum);
        pageQuery.getQuery().put("pageSize", rowNum);
        PageResult<LabConfigSpecimenEntity> result = new PageResult(rowNum);
        var filters = SearchFilterEngine.generateQueryList(pageQuery.getFilters(),LabConfigSpecimenEntity.class);
        result.setRecords(dao.queryCountByCondition(pageQuery.getQuery(),filters));
        result.setPage(page);
        result.setData(dao.queryPage(pageQuery.getQuery(),filters));
        return result;
    }


    @Override
    public LabConfigSpecimenEntity findBySn(String sn) {
        return dao.findBySN(sn);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer saveLabConfigSpecimen(Map<String, Object> model, String sn) throws Exception {
        Integer result = 0;
        LabConfigSpecimenEntity entity = null;
        if (Tl.isEmpty(sn)) {
            //执行保存
            entity = new LabConfigSpecimenEntity();
            Tl.getChangeLog(entity, model);
            entity.setPYFieldValue();
            entity.setSn(queryNewSN(LabConfigSpecimenEntity.class));
            result = dao.insertNew(entity);
            if (result > 0) {
                writeLog(ActionType.CREATE, entity.getSn(), "插入试配试件信息信息", Tl.toJson(entity), LabConfigSpecimenEntity.class);
            }

        } else {
            //执行修改
            entity = findBySn(sn);
            Tl.getChangeLog(entity, model);
            entity.setPYFieldValue();
            result = dao.update(entity);
            if (result > 0) {
                writeLog(ActionType.UPDATE, sn, "更新试配试件信息信息", Tl.toJson(entity), LabConfigSpecimenEntity.class);
            }
        }

        return result;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer delete(String sn) {
        Integer result = dao.changeStateBySN(sn, DataState.DELETED.code);
        if (result > 0) {
            writeLog(ActionType.DELETE, sn, LabConfigSpecimenEntity.class);
        }
        return result;
    }
}